function An = slnrmvecs(A, p, d)
%SLNORMALIZE Normalize the vectors in an array
%
% [ Syntax ]
%   - An = slnrmvecs(A)
%   - An = slnrmvecs(A, p)
%   - An = slnrmvecs(A, [], d)
%   - An = slnrmvecs(A, p, d)
%
% [ Arguments ]
%   - A:        the input array
%   - p:        the order of the norm (default = 2)
%   - d:        the dimension along which the vectors are normalized
%   - An:       the array with the vectors normalized
%
% [ Description ]
%   - An = slnrmvecs(A) normalizes the column vectors of A by 2nd-order.
%
%   - An = slnrmvecs(A, p) normalizes the column vectors of a by
%     pth-order.
%
%   - An = slnrmvecs(A, [], d) normalizes the vectors of A along the
%     specified dimension by 2nd-order.
%
%   - An = slnrmvecs(A, p, d) normalizes the vectors of A along the
%     specified dimension by pth-order.
%
% [ Remarks ]
%   # Normalize an array by pth order means dividing the elements of 
%     the array by its p-th norm.
%   # p can be inf or -inf. If p = inf, then the Lp norm is simply the
%     maximum magnitude value; while if p = -inf, then the Lp norm is the 
%     minimum magnitude value.
%
% [ History ]
%   - Created by Dahua Lin on Nov 19th, 2005
%   - Modified by Dahua Lin on Sep 10th, 2006
%       - replace slmul by slmulvec to increase efficiency
%   - Modified by Dahua Lin, on Jul 2nd, 2007
%       - Change the name from slnormalize to slnrmvecs,
%         which focuses on normalizing vectors.
%       - Rewrite the core based on bsxfun.
%       - Change the strategy to deal with near-zero norms, to
%         improve the accuracy for small values.
%   

%% parse and verify input arguments
if nargin < 2 || isempty(p)
    p = 2;
end
if nargin < 3 || isempty(d)
    d = 1;
end
if ~isnumeric(p) || ~isscalar(p) || p == 0
    error('sltoolbox:slvecnorm:illegalarg', 'p should be a non-zero numeric scalar');
end

%% main 

nrms = slvecnorm(A, p, d);
nrms(nrms == 0) = 1;

An = bsxfun(@times, A, 1 ./ nrms);

